﻿#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using System;
using System.Text;
using System.Collections.Generic;
using System.Reflection;
using ILRuntime.Runtime.Enviorment;
using Smart.Model;
using Smart.Hotfix;
using System.IO;

namespace Smart.Editor
{
    [System.Reflection.Obfuscation(Exclude = true)]
    public class ILRuntimeCLRBinding
    {
        //[MenuItem("Tools/ILRuntime/Generate Cross Binding Adapter")]
        static void GenerateCrossbindAdapter()
        {
            //由于跨域继承特殊性太多，自动生成无法实现完全无副作用生成，所以这里提供的代码自动生成主要是给大家生成个初始模版，简化大家的工作
            //大多数情况直接使用自动生成的模版即可，如果遇到问题可以手动去修改生成后的文件，因此这里需要大家自行处理是否覆盖的问题
            using (StreamWriter sw = new StreamWriter("Assets/Samples/ILRuntime/1.6/Demo/Scripts/Examples/04_Inheritance/InheritanceAdapter.cs"))
            {
                //sw.WriteLine(ILRuntime.Runtime.Enviorment.CrossBindingCodeGenerator.GenerateCrossBindingAdapterCode(typeof(TestClassBase), "Smart.Model"));
            }

            AssetDatabase.Refresh();
        }

        [MenuItem("Tools/ILRuntime/Generate CLR Binding Code by Analysis")]
        static void GenerateCLRBindingByAnalysis()
        {
            //用新的分析热更dll调用引用来生成绑定代码
            ILRuntime.Runtime.Enviorment.AppDomain domain = new ILRuntime.Runtime.Enviorment.AppDomain();
            using (System.IO.FileStream fs = new System.IO.FileStream("Assets/Res/Code/Hotfix.dll.bytes", System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                domain.LoadAssembly(fs);
                //Crossbind Adapter is needed to generate the correct binding code
                ILHelper.InitILRuntime(domain);
                ILRuntime.Runtime.CLRBinding.BindingCodeGenerator.GenerateBindingCode(domain, "Assets/Scripts/Model/ILBinding");
                AssetDatabase.Refresh();
            }
        }

        [MenuItem("Tools/ILRuntime/ClearCLRBinding")]
        static void ClearCLRBinding()
        {
            var src = Application.dataPath + "/../CodeTemplate/CLRBindings.cs";
            var dst = Application.dataPath + "/Model/ILBinding/CLRBindings.cs";
            var dstFolder = Application.dataPath + "/Model/ILBinding/";
            try
            {
                DirectoryInfo dir = new DirectoryInfo(dstFolder);
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录
                foreach (FileSystemInfo i in fileinfo)
                {
                    if (i is DirectoryInfo)            //判断是否文件夹
                    {
                        DirectoryInfo subdir = new DirectoryInfo(i.FullName);
                        subdir.Delete(true);          //删除子目录和文件
                    }
                    else
                    {
                        File.Delete(i.FullName);      //删除指定文件
                    }
                }
            }
            catch (Exception e)
            {
                throw;
            }
            System.IO.File.Copy(src, dst,true);
            AssetDatabase.Refresh();
        }
    }
}
#endif
